package zucc.edu.cn.chengchengparttime.Mysignup;

import zucc.edu.cn.chengchengparttime.LoginAndReg.LoginActivity;
import zucc.edu.cn.chengchengparttime.R;
import zucc.edu.cn.chengchengparttime.Signup;
import zucc.edu.cn.chengchengparttime.Text;
import zucc.edu.cn.chengchengparttime.dbHomeHelper;

import android.app.Fragment;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class MysignupFragment extends Fragment {
    private static final String DEBUG_TAG="Chengcheng Log";
    private SQLiteDatabase mDatabase;// 数据库实例
    dbHomeHelper homeHelper;

    private RecyclerView rv;
    private MysignupAdapter mysignupAdapter;
    private Context context;
    private List<Signup> data=new ArrayList<>(); //数据列表
    private ImageView line1,line2,line3,line4;
    private TextView text1,text2,text3,text4;
    private TextView textall,textsignuped,textemployed,textfinished;
    String userid=LoginActivity.getCurrentUser(); //获取当前登录用户

    public MysignupFragment(){}

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState){
        View view = inflater.inflate(R.layout.fragment_mysignup, container, false);
        context = this.getActivity();
        rv = view.findViewById(R.id.rv);

        mDatabase=SQLiteDatabase.openDatabase("/data/user/0/zucc.edu.cn.chengchengparttime/databases/Chengcheng.db",null,SQLiteDatabase.OPEN_READWRITE);
        Log.d("Chengcheng.db","获取数据库"+mDatabase);

        textall=view.findViewById(R.id.all);
        textsignuped=view.findViewById(R.id.signuped);
        textemployed=view.findViewById(R.id.employed);
        textfinished=view.findViewById(R.id.finished);

        line1=view.findViewById(R.id.underline1);
        line2=view.findViewById(R.id.underline2);
        line3=view.findViewById(R.id.underline3);
        line4=view.findViewById(R.id.underline4);
        text1=view.findViewById(R.id.all);
        text2=view.findViewById(R.id.signuped);
        text3=view.findViewById(R.id.employed);
        text4=view.findViewById(R.id.finished);

        Query(mDatabase,view,"全部");
        textall.setOnClickListener(view1 -> {
            Query(mDatabase,view,"全部");
        });
        textsignuped.setOnClickListener(view1 -> {
            Query(mDatabase,view,"已报名");
        });
        textemployed.setOnClickListener(view1 -> {
            Query(mDatabase,view,"已录取");
        });
        textfinished.setOnClickListener(view1 -> {
            Query(mDatabase,view,"已结束");
        });

        return view;
    }

    public void Query(SQLiteDatabase mDatabase,View view,String state){
        if(state=="全部"){
            data.clear(); //清屏
            Cursor c=mDatabase.query("tbl_signup", null, "UserID=?", new String[]{userid + ""}, null, null,"SignupStatus DESC");
            c.moveToFirst();//游标移到第一条记录

            if(c.getCount()!=0){
                // 打印记录
                String[] x = new String[30];
                while(!c.isAfterLast()){
                    String rowResults = "||";
                    for (int i = 0; i < c.getColumnCount(); i++) {
                        rowResults = rowResults.concat(c.getString(i) + "||");
                        x[i]=c.getString(i);
                    }
                    Signup signup=new Signup(Long.parseLong(x[0]),x[1],x[2],x[3],x[4],x[5],x[6],Double.parseDouble(x[7]),x[8],x[9],x[10],x[11],x[12]);
                    data.add(signup);
                    Log.d("signup",signup.toString());//测试

                    c.moveToNext();
                }
            }
            Log.d(DEBUG_TAG,"查询全部成功");
            c.close();//关闭游标工厂

            //适配器的定义与设置
            mysignupAdapter=new MysignupAdapter(data,context,mDatabase,view);//传view用于刷新页面
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.getActivity());
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            rv.setLayoutManager(linearLayoutManager);
            rv.setAdapter(mysignupAdapter);

            line1.setVisibility(View.VISIBLE);
            line2.setVisibility(View.VISIBLE);
            line3.setVisibility(View.VISIBLE);
            line4.setVisibility(View.VISIBLE);
            line2.setVisibility(View.GONE);
            line3.setVisibility(View.GONE);
            line4.setVisibility(View.GONE);

            text2.getPaint().setFakeBoldText(false);
            text3.getPaint().setFakeBoldText(false);
            text4.getPaint().setFakeBoldText(false);
            text1.getPaint().setFakeBoldText(true); //字体动态设置加粗
            text1.postInvalidate();
            text2.postInvalidate();
            text3.postInvalidate();
            text4.postInvalidate();
        }else if(state=="已报名"){
            data.clear(); //清屏
            Cursor c=mDatabase.query("tbl_signup", null, "UserID=? and SignupStatus=?", new String[]{userid,state}, null, null,"SignupStatus");
            c.moveToFirst();//游标移到第一条记录

            if(c.getCount()!=0){
                // 打印记录
                String[] x = new String[30];
                while(!c.isAfterLast()){
                    String rowResults = "||";
                    for (int i = 0; i < c.getColumnCount(); i++) {
                        rowResults = rowResults.concat(c.getString(i) + "||");
                        x[i]=c.getString(i);
                    }
                    Signup signup=new Signup(Long.parseLong(x[0]),x[1],x[2],x[3],x[4],x[5],x[6],Double.parseDouble(x[7]),x[8],x[9],x[10],x[11],x[12]);
                    data.add(signup);
                    Log.d("signup",signup.toString());//测试

                    c.moveToNext();
                }
            }
            Log.d(DEBUG_TAG,"查询已报名成功");
            c.close();//关闭游标工厂

            //适配器的定义与设置
            mysignupAdapter=new MysignupAdapter(data,context,mDatabase,view);
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.getActivity());
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            rv.setLayoutManager(linearLayoutManager);
            rv.setAdapter(mysignupAdapter);

            line1.setVisibility(View.VISIBLE);
            line2.setVisibility(View.VISIBLE);
            line3.setVisibility(View.VISIBLE);
            line4.setVisibility(View.VISIBLE);
            line1.setVisibility(View.GONE);
            line3.setVisibility(View.GONE);
            line4.setVisibility(View.GONE);

            text1.getPaint().setFakeBoldText(false);
            text3.getPaint().setFakeBoldText(false);
            text4.getPaint().setFakeBoldText(false);
            text2.getPaint().setFakeBoldText(true); //字体动态设置加粗
            text1.postInvalidate();
            text2.postInvalidate();
            text3.postInvalidate();
            text4.postInvalidate();
        }else if(state=="已录取"){
            data.clear(); //清屏
            Cursor c=mDatabase.query("tbl_signup", null, "UserID=? and SignupStatus=?", new String[]{userid,state}, null, null,"SignupStatus");
            c.moveToFirst();//游标移到第一条记录

            if(c.getCount()!=0){
                // 打印记录
                String[] x = new String[30];
                while(!c.isAfterLast()){
                    String rowResults = "||";
                    for (int i = 0; i < c.getColumnCount(); i++) {
                        rowResults = rowResults.concat(c.getString(i) + "||");
                        x[i]=c.getString(i);
                    }
                    Signup signup=new Signup(Long.parseLong(x[0]),x[1],x[2],x[3],x[4],x[5],x[6],Double.parseDouble(x[7]),x[8],x[9],x[10],x[11],x[12]);
                    data.add(signup);
                    Log.d("signup",signup.toString());//测试

                    c.moveToNext();
                }
            }
            Log.d(DEBUG_TAG,"查询已录取成功");
            c.close();//关闭游标工厂

            //适配器的定义与设置
            mysignupAdapter=new MysignupAdapter(data,context,mDatabase,view);
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.getActivity());
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            rv.setLayoutManager(linearLayoutManager);
            rv.setAdapter(mysignupAdapter);

            line1.setVisibility(View.VISIBLE);
            line2.setVisibility(View.VISIBLE);
            line3.setVisibility(View.VISIBLE);
            line4.setVisibility(View.VISIBLE);
            line1.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);
            line4.setVisibility(View.GONE);

            text1.getPaint().setFakeBoldText(false);
            text2.getPaint().setFakeBoldText(false);
            text4.getPaint().setFakeBoldText(false);
            text3.getPaint().setFakeBoldText(true); //字体动态设置加粗
            text1.postInvalidate();
            text2.postInvalidate();
            text3.postInvalidate();
            text4.postInvalidate();
        }else if(state=="已结束"){
            data.clear(); //清屏
            Cursor c=mDatabase.query("tbl_signup", null, "UserID=? and SignupStatus=?", new String[]{userid,state}, null, null,"SignupStatus");
            c.moveToFirst();//游标移到第一条记录

            if(c.getCount()!=0){
                // 打印记录
                String[] x = new String[30];
                while(!c.isAfterLast()){
                    String rowResults = "||";
                    for (int i = 0; i < c.getColumnCount(); i++) {
                        rowResults = rowResults.concat(c.getString(i) + "||");
                        x[i]=c.getString(i);
                    }
                    Signup signup=new Signup(Long.parseLong(x[0]),x[1],x[2],x[3],x[4],x[5],x[6],Double.parseDouble(x[7]),x[8],x[9],x[10],x[11],x[12]);
                    data.add(signup);
                    Log.d("signup",signup.toString());//测试

                    c.moveToNext();
                }
            }
            Log.d(DEBUG_TAG,"查询已结束成功");
            c.close();//关闭游标工厂

            //适配器的定义与设置
            mysignupAdapter=new MysignupAdapter(data,context,mDatabase,view);
            LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this.getActivity());
            linearLayoutManager.setOrientation(RecyclerView.VERTICAL);
            rv.setLayoutManager(linearLayoutManager);
            rv.setAdapter(mysignupAdapter);

            line1.setVisibility(View.VISIBLE);
            line2.setVisibility(View.VISIBLE);
            line3.setVisibility(View.VISIBLE);
            line4.setVisibility(View.VISIBLE);
            line1.setVisibility(View.GONE);
            line3.setVisibility(View.GONE);
            line2.setVisibility(View.GONE);

            text1.getPaint().setFakeBoldText(false);
            text2.getPaint().setFakeBoldText(false);
            text3.getPaint().setFakeBoldText(false);
            text4.getPaint().setFakeBoldText(true); //字体动态设置加粗
            text1.postInvalidate();
            text2.postInvalidate();
            text3.postInvalidate();
            text4.postInvalidate();
        }
    }
}
